﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PlugNT.Cms.Core;
using PlugNT.Cms.Model;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Page;
using FJASPX.Service;
using FJASPX.Domain;

namespace PlugNT.Cms.Web.Admin.Pre_Product
{

    public partial class Admin : AdminPage
    {
        private const int STATE_HOT = 2;//热门
        public IProductService iProductService { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();
             switch (Request["t"])
            {
                case "hot":
                    Product product = iProductService.Get(Convert.ToInt32(Request["id"]));
                    if (product != null)
                    {
                        if (!Request["commend"].Equals(Convert.ToString(STATE_HOT)))
                        {
                            product.AddState(STATE_HOT);
                        }
                        else
                        {
                            product.RemoveState(STATE_HOT);
                        }
                        iProductService.Update(product);
                    }
                    break;
            }
            if (!IsPostBack)
            {
                BindData();
            }
        }

        #region 数据绑定
        private void BindData()
        {


            this.Repeater_admin.DataSource = iProductService.GetAll("TimeStamp", true);
            this.Repeater_admin.DataBind();

        }
        #endregion

        #region 显示数据处理
        protected void Repeater_admin_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            //{
            //    Literal LitFirst = (Literal)e.Item.FindControl("LitFirst");
            //    Label LabClassNm = (Label)e.Item.FindControl("LabClassName");

            //    string LitStyle = "<span style=\"width:{0}px; text-align:right; vertical-align:middle; line-height:25px; height:25px;\">{1}{2}</span>";
            //    string LitImg1 = "<img src=../images/openfolder.gif align=absmiddle />";
            //    string LitImg2 = "<img src=../images/folder.gif align=absmiddle />";
            //    string LitImg3 = "<img src=../images/t.gif align=absmiddle />";
            //    Product product = (Product)e.Item.DataItem;
            //    int clsListLen = Convert.ToInt32(product.Cid);

            //    if (clsListLen == 0)
            //    {
            //        LabClassNm.Font.Bold = true;
            //        LitFirst.Text = LitImg1;
            //    }
            //    else
            //    {
            //        LitFirst.Text = string.Format(LitStyle, clsListLen * 20, LitImg3, LitImg2);
            //    }
            //}
        }
        #endregion

        #region 编辑删除处理
        protected void Repeater_admin_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidClassId = (HiddenField)e.Item.FindControl("hidClassId");
            HiddenField hidClassTag = (HiddenField)e.Item.FindControl("hidClassTag");
            Label LabClassNm = (Label)e.Item.FindControl("LabClassName");
            try
            {
                switch (e.CommandName.ToLower())
                {
                    case "btndelete":
                        iProductService.DeleteById(Int32.Parse(hidClassId.Value));
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "ajaxjs", "alert('所选产品删除成功！')", true);
                        BindData();
                        break;
                }

            }
            catch (Exception ex) { Page.ClientScript.RegisterStartupScript(Page.GetType(), "ajaxjs", string.Format("alert('删除操作失败，请与管理员联系！{0}')", ex), true); }
        }
        #endregion

        //批量删除
        protected void ImageButton_all_del_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {

            int nub = 0;
            for (int i = 0; i < Repeater_admin.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)Repeater_admin.Items[i].FindControl("hidid")).Value.Trim());
                CheckBox CK = (CheckBox)Repeater_admin.Items[i].FindControl("ID_list");
                if (CK.Checked)
                {
                    iProductService.DeleteById(id);

                }
            }
            Response.Redirect("../Message.aspx?message=批量删除<b>" + nub + "</b>条信息成功！&z=yes");

        }
    }
}
